package com.iqik.rpc.handler;

import com.iqik.rpc.service.UserService;
import com.iqik.rpc.service.UserServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg.toString().startsWith("UserService")) {
            // 如果符合规则，调用实现类获取一个result
            UserService userService = new UserServiceImpl();
            String result = userService.sayHello(msg.toString().substring(msg.toString().lastIndexOf("#") + 1));
            // 把调用实现类的方法获取到的接过写回到客户端
            ctx.writeAndFlush(result + " I'm " + ctx.channel().localAddress().toString());
        }
    }
}
